package com.ctshk.rpc.order.goods.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.goods.dto.OrderCommodityPageListDetailDTO;
import com.ctshk.rpc.order.goods.dto.OrderDetailDTO;
import com.ctshk.rpc.order.goods.dto.OrderPageListDTO;
import com.ctshk.rpc.order.goods.entity.Order;
import com.ctshk.rpc.order.goods.mapper.provider.OrderProvider;
import com.ctshk.rpc.order.goods.req.OrderPageListReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
public interface OrderMapper extends BaseMapper<Order> {

    @SelectProvider(type = OrderProvider.class, method = "findOrderPageByCondition")
    IPage<OrderPageListDTO> findOrderPageByCondition(IPage<OrderPageListDTO> page, OrderPageListReq pageReq);

    @Select({
            "<script>",
            "SELECT",
            "t.id,t.gmt_create,t.pay_expire_date,t.big_client_name,t.receipt_up,t.order_notes,t.sale_type," +
                    "t.business_type,t.product_type,t.payment_status,t.scheduled_status,t.cancel_status," +
                    "t.sale_user_id,t.sale_user_name,t.order_number,t.user_id,t.user_name,t.order_amount," +
                    "t.freight_amount,t.tax_amount,t.source_id,t.source_name,t.delivery_mode,t.refund_service_charge_type," +
                    "t.refund_service_charge ",
            ",oci.consignee,oci.consignee_phone,oci.consignee_email,oci.consignee_address",
            ",osi.shipper,osi.shipper_phone,osi.shipper_store_id,osi.shipper_store_name,osi.shipper_store_address",
            "FROM `ctshk-goods-order`.t_order t",
            "LEFT JOIN `ctshk-goods-order`.t_order_shipper_info osi ON osi.order_id = t.id",
            "LEFT JOIN `ctshk-goods-order`.t_order_consignee_info oci ON oci.order_id = t.id",
            "WHERE t.is_deleted = 0 AND t.id = #{orderId}",
            "</script>"
    })
    OrderDetailDTO findOrderCustomerInfoByOrderId(@Param("orderId") Long orderId);

    @Select({
            "<script>",
             "SELECT c.commodity_id commodityId, SUM(c.number) number FROM `ctshk-goods-order`.t_order o JOIN `ctshk-goods-order`.t_order_commodity c ON o.id = c.order_id GROUP BY c.commodity_id",
            "</script>"
    })
    List<OrderCommodityPageListDetailDTO> salesVolume();

    /**
     * 根据查询号码和订单编号查询订单id
     * @param orderNumber 订单编号
     * @param searchCode 查询号码
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_order WHERE order_number = #{orderNumber} AND search_code = #{searchCode}"
    })
    Order findOrderIdByOrderNumerAndSearchCode(@Param("orderNumber") String orderNumber,@Param("searchCode") String searchCode);

    @Insert({
            "<script>" +
            "INSERT INTO `ctshk-goods-order`.t_order  " +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"+
                    "<if test='id != null'>id,</if>" +
                    "<if test='orderNumber != null'>order_number,</if>" +
                    "<if test='saleType != null'>sale_type,</if>" +
                    "<if test='paymentStatus != null'>payment_status,</if>" +
                    "<if test='scheduledStatus != null'>scheduled_status,</if>" +
                    "<if test='commodityName != null'>commodity_name,</if>" +
                    "<if test='businessType != null'>business_type,</if>" +
                    "<if test='productType != null'>product_type,</if>" +
                    "<if test='sourceId != null'>source_id,</if>" +
                    "<if test='sourceChannel != null'>source_channel,</if>" +
                    "<if test='sourceType != null'>source_type,</if>" +
                    "<if test='sourceName != null'>source_name,</if>" +
                    "<if test='userName != null'>user_name,</if>" +
                    "<if test='deliveryMode != null'>delivery_mode,</if>" +
                    "<if test='orderNotes != null'>order_notes,</if>" +
                    "<if test='orderStatus != null'>order_status,</if>" +
                    "<if test='orderWriteOffNumber != null'>order_write_off_number,</if>" +
                    "<if test='writeOffStatus != null'>write_off_status,</if>" +
                    "<if test='isDeleted != null'>is_deleted,</if>" +
                    "<if test='searchCode != null'>search_code,</if>" +
                    "<if test='shopId != null'>shop_id,</if>" +
                    "<if test='receiptUp != null'>receipt_up,</if>" +
                    "<if test='payExpireDate != null'>pay_expire_date,</if>" +
                    "<if test='gmtCreate != null'>gmt_create,</if>" +
                    "<if test='gmtModified != null'>gmt_modified,</if>" +
                    "</trim>" +
                    "VALUES" +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
                    "<if test='id != null'>#{id},</if>" +
                    "<if test='orderNumber != null'>#{orderNumber},</if>" +
                    "<if test='saleType != null'>#{saleType},</if>" +
                    "<if test='paymentStatus != null'>#{paymentStatus},</if>" +
                    "<if test='scheduledStatus != null'>#{scheduledStatus},</if>" +
                    "<if test='commodityName != null'>#{commodityName},</if>" +
                    "<if test='businessType != null'>#{businessType},</if>" +
                    "<if test='productType != null'>#{productType},</if>" +
                    "<if test='sourceId != null'>#{sourceId},</if>" +
                    "<if test='sourceChannel != null'>#{sourceChannel},</if>" +
                    "<if test='sourceType != null'>#{sourceType},</if>" +
                    "<if test='sourceName != null'>#{sourceName},</if>" +
                    "<if test='userName != null'>#{userName},</if>" +
                    "<if test='deliveryMode != null'>#{deliveryMode},</if>" +
                    "<if test='orderNotes != null'>#{orderNotes},</if>" +
                    "<if test='orderStatus != null'>#{orderStatus},</if>" +
                    "<if test='orderWriteOffNumber != null'>#{orderWriteOffNumber},</if>" +
                    "<if test='writeOffStatus != null'>#{writeOffStatus},</if>" +
                    "<if test='isDeleted != null'>#{isDeleted},</if>" +
                    "<if test='searchCode != null'>#{searchCode},</if>" +
                    "<if test='shopId != null'>#{shopId},</if>" +
                    "<if test='receiptUp != null'>#{receiptUp},</if>" +
                    "<if test='payExpireDate != null'>#{payExpireDate},</if>" +
                    "<if test='gmtCreate != null'>#{gmtCreate},</if>" +
                    "<if test='gmtModified != null'>#{gmtModified},</if>" +
                    "</trim>"+
            "</script>"
    })
    void insertOrder(Order order);


    //UPDATE `ctshk-goods-order`.`t_order` SET
    // `id` = 1384761942896545793, `order_number` = 'SCODE1344284712813568210425298',
    // `order_amount` = 563,
    // `sale_type` = 1,
    // `payment_status` = 1,
    // `scheduled_status` = 1,
    // `commodity_name` = '玩具小店-update0415-0930',
    // `approval_id` = NULL,
    // `cancel_status` = NULL,
    // `business_type` = 1,
    // `product_type` = 1,
    // `source_id` = 12345678,
    // `source_channel` = 1,
    // `source_type` = 3,
    // `source_name` = '订单来源名称',
    // `sale_user_id` = NULL,
    // `sale_user_name` = NULL,
    // `user_id` = NULL,
    // `user_name` = NULL,
    // `freight_amount` = NULL,
    // `tax_amount` = NULL,
    // `delivery_mode` = 2,
    // `order_notes` = '订单备注',
    // `order_status` = 1,
    // `order_write_off_number` = NULL,
    // `write_off_status` = 1,
    // `write_off_time` = NULL,
    // `promotion_code_id` = NULL,
    // `promotion_code` = NULL,
    // `promotion_code_commodity_id` = NULL,
    // `gmt_create` = '2021-04-21 14:52:29',
    // `create_id` = 1344284712813568,
    // `gmt_modified` = '2021-04-21 14:52:29',
    // `modified_id` = 1344284712813568,
    // `is_deleted` = 0,
    // `search_code` = NULL,
    // `shop_id` = NULL
    // WHERE `id` = 1384761942896545793 AND `order_number` = 'SCODE1344284712813568210425298' AND `order_amount` = 563 AND `sale_type` = 1 AND `payment_status` = 1 AND `scheduled_status` = 1 AND `commodity_name` = '玩具小店-update0415-0930' AND `approval_id` IS NULL AND `cancel_status` IS NULL AND `business_type` = 1 AND `product_type` = 1 AND `source_id` = 12345678 AND `source_channel` = 1 AND `source_type` = 3 AND `source_name` = '订单来源名称' AND `sale_user_id` IS NULL AND `sale_user_name` IS NULL AND `user_id` IS NULL AND `user_name` IS NULL AND `freight_amount` IS NULL AND `tax_amount` IS NULL AND `delivery_mode` = 2 AND `order_notes` = '订单备注' AND `order_status` = 1 AND `order_write_off_number` IS NULL AND `write_off_status` = 1 AND `write_off_time` IS NULL AND `promotion_code_id` IS NULL AND `promotion_code` IS NULL AND `promotion_code_commodity_id` IS NULL AND `gmt_create` = Cast('2021-04-21 14:52:29' AS Binary(19)) AND `create_id` = 1344284712813568 AND `gmt_modified` = Cast('2021-04-21 14:52:29' AS Binary(19)) AND `modified_id` = 1344284712813568 AND `is_deleted` = 0 AND `search_code` IS NULL AND `shop_id` IS NULL LIMIT 1;
    @Update({
        "<script>" +
                "UPDATE `ctshk-goods-order`.`t_order`\n" +
                " <trim prefix=\"set\" suffixOverrides=\",\">\n" +
                "  <if test=\"shopId!=null\">`shop_id` = #{shopId}, </if>\n" +
                "  <if test=\"promotionCodeCommodityId!=null\">`promotion_code_commodity_id` = #{promotionCodeCommodityId}, </if>\n" +
                "  <if test=\"gmtCreate!=null\">`gmt_create` = #{gmtCreate}, </if>\n" +
                "  <if test=\"createId!=null\">`create_id` = #{createId}, </if>\n" +
                "  <if test=\"gmtModified!=null\">`gmt_modified` = #{gmtModified}, </if>\n" +
                "  <if test=\"modifiedId!=null\">`modified_id` = #{modifiedId}, </if>\n" +
                "  <if test=\"isDeleted!=null\">`is_deleted` = #{isDeleted}, </if>\n" +
                "  <if test=\"searchCode!=null\">`search_code` = #{searchCode}, </if>\n" +

                "  <if test=\"orderNotes!=null\">`order_notes` = #{orderNotes}, </if>\n" +
                "  <if test=\"orderStatus!=null\">`order_status` = #{orderStatus}, </if>\n" +
                "  <if test=\"orderWriteOffNumber!=null\">`order_write_off_number` = #{orderWriteOffNumber}, </if>\n" +
                "  <if test=\"writeOffStatus!=null\">`write_off_status` = #{writeOffStatus}, </if>\n" +
                "  <if test=\"writeOffTime!=null\">`write_off_time` = #{writeOffTime}, </if>\n" +
                "  <if test=\"promotionCodeId!=null\">`promotion_code_id` = #{promotionCodeId}, </if>\n" +
                "  <if test=\"promotionCode!=null\">`promotion_code` = #{promotionCode}, </if>\n" +

                "  <if test=\"orderNumber!=null\">`order_number` = #{orderNumber}, </if>\n" +
                "  <if test=\"orderAmount!=null\">`order_amount` = #{orderAmount}, </if>\n" +
                "  <if test=\"saleType!=null\">`sale_type` = #{saleType}, </if>\n" +
                "  <if test=\"paymentStatus!=null\">`payment_status` = #{paymentStatus}, </if>\n" +
                "  <if test=\"scheduledStatus!=null\">`scheduled_status` = #{scheduledStatus}, </if>\n" +
                "  <if test=\"commodityName!=null\">`commodity_name` = #{commodityName}, </if>\n" +
                "  <if test=\"approvalId!=null\">`approval_id` = #{approvalId}, </if>\n" +
                "  <if test=\"cancelStatus!=null\">`cancel_status` = #{cancelStatus}, </if>\n" +
                "  <if test=\"businessType!=null\">`business_type` = #{businessType}, </if>\n" +
                "  <if test=\"productType!=null\">`product_type` = #{productType}, </if>\n" +
                "  <if test=\"sourceId!=null\">`source_id` = #{sourceId}, </if>\n" +
                "  <if test=\"sourceChannel!=null\">`source_channel` = #{sourceChannel}, </if>\n" +
                "  <if test=\"sourceType!=null\">`source_type` = #{sourceType}, </if>\n" +
                "  <if test=\"sourceName!=null\">`source_name` = #{sourceName}, </if>\n" +
                "  <if test=\"saleUserId!=null\">`sale_user_id` = #{saleUserId}, </if>\n" +
                "  <if test=\"saleUserName!=null\">`sale_user_name` = #{saleUserName}, </if>\n" +
                "  <if test=\"userId!=null\">`user_id` = #{userId}, </if>\n" +
                "  <if test=\"userName!=null\">`user_name` = #{userName}, </if>\n" +
                "  <if test=\"freightAmount!=null\">`freight_amount` = #{freightAmount}, </if>\n" +
                "  <if test=\"taxAmount!=null\">`tax_amount` = #{taxAmount}, </if>\n" +
                "  <if test=\"deliveryMode!=null\">`delivery_mode` = #{deliveryMode}, </if>\n" +
                "  <if test=\"payMethod!=null\">`pay_method` = #{payMethod}, </if>\n" +
                " </trim>\n" +
                " WHERE id=#{id}" +
                "</script>"
    })
    void updateOrderById(Order order);

    @Select({
            "SELECT id,pay_method,shop_id,order_source_type,receipt_up,pay_expire_date,order_number,order_amount,sale_type," +
                    "payment_status,scheduled_status,commodity_name,approval_id,cancel_status," +
                    "business_type,product_type,source_id,source_channel,source_type,source_name," +
                    "sale_user_id,sale_user_name,user_id,user_name,freight_amount,tax_amount," +
                    "delivery_mode,order_notes,order_status,order_write_off_number,write_off_status," +
                    "write_off_time,promotion_code_id,promotion_code,promotion_code_commodity_id," +
                    "create_id,modified_id,is_deleted,search_code,shop_id," +
                    "receipt_up,gmt_create,gmt_modified " +
                    "FROM `ctshk-goods-order`.t_order " +
                    "WHERE id=#{id}"
    })
    Order selectOrderById(@Param("id") Long oderId);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     * @return
     */
    @Select({
            "SELECT  id,pay_method,order_number,order_amount,sale_type,payment_status,scheduled_status,commodity_name,approval_id,cancel_status,business_type,product_type,source_id,source_channel,source_type,source_name,sale_user_id,sale_user_name,user_id,user_name,freight_amount,tax_amount,delivery_mode,order_notes,order_status,order_write_off_number,write_off_status,write_off_time,promotion_code_id,promotion_code,promotion_code_commodity_id,create_id,modified_id,is_deleted,search_code,shop_id,receipt_up,pay_expire_date,gmt_create,gmt_modified  " +
                    "FROM `ctshk-goods-order`.t_order     " +
                    "WHERE (order_number = #{orderNumber} AND is_deleted = 0)"
    })
    Order selectOrderByOrderNumber(@Param("orderNumber") String orderNumber);
}
